﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKElementFinish.ColumnSurface.DataDefinition;
using YJKElementFinish.ColumnSurface.Managers;
using YJKElementFinish.MultiWallEdit.DataDefinition;

namespace YJKElementFinish.MultiWallEdit.Forms
{
	public partial class WallTypeMergeForm : System.Windows.Forms.Form
	{
		public WallTypeMergeForm(List<List<WallType>> userSelectTypeGroupList, Document doc)
		{
			this.InitializeComponent();
			this.m_Doc = doc;
			this.InitializeWallGroupInfoList(userSelectTypeGroupList);
		}

		public List<List<List<WallTypeLayerInfos>>> GetCombinationInfosByGroup()
		{
			List<List<List<WallTypeLayerInfos>>> list = new List<List<List<WallTypeLayerInfos>>>();
			for (int i = 0; i < this.m_UserSelectTypeGroupList.Count; i++)
			{
				List<List<WallTypeLayerInfos>> currentWallTypeGroupInfos = this.GetCurrentWallTypeGroupInfos(i);
				list.Add(currentWallTypeGroupInfos);
			}
			return list;
		}

		private void InitializeWallGroupInfoList(List<List<WallType>> userSelectTypeGroupList)
		{
			int num = 0;
			foreach (List<WallType> list in userSelectTypeGroupList)
			{
				int num2 = 0;
				List<List<int>> list2 = new List<List<int>>();
				List<WallTypeLayerInfos> list3 = new List<WallTypeLayerInfos>();
				foreach (WallType wallType in list)
				{
					WallTypeLayerInfos wallLayerInfoListByType = WallTypeManager.GetWallLayerInfoListByType(wallType, this.m_Doc);
					list3.Add(wallLayerInfoListByType);
					list2.Add(new List<int>
					{
						num2
					});
					num2++;
				}
				string groupName = "组" + (num + 1).ToString();
				WallTypeGroupInfos item = new WallTypeGroupInfos(groupName, list3);
				this.m_UserSelectTypeGroupList.Add(item);
				this.m_OperationTypeByTypeList.Add(WallTypeMergeForm.OperationType.OT_NotMerge);
				this.m_GroupCombinationInfosList.Add(list2);
				num++;
			}
		}

		private void WallTypeMergeForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.UpdateUserSelectTreeView();
			this.SetCurrentOperationType(WallTypeMergeForm.OperationType.OT_NotMerge);
			this.UpdateUserNewTreeView(false);
		}

		private void WallTypeMergeForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void SetCurrentOperationType(WallTypeMergeForm.OperationType operationType)
		{
			if (operationType == WallTypeMergeForm.OperationType.OT_NotMerge)
			{
				this.rdbNotMerge.Checked = true;
			}
			else
			{
				if (operationType != WallTypeMergeForm.OperationType.OT_MergeToOne)
				{
					this.rdbCustom.Checked = true;
					this.btnCombination.Enabled = true;
					this.btnDecomposition.Enabled = true;
					this.treeViewUserNew.CheckBoxes = true;
					this.treeViewUserNew.DrawMode = TreeViewDrawMode.OwnerDrawAll;
					return;
				}
				this.rdbMergeToOne.Checked = true;
			}
			this.btnCombination.Enabled = false;
			this.btnDecomposition.Enabled = false;
			this.treeViewUserNew.CheckBoxes = false;
			this.treeViewUserNew.DrawMode = TreeViewDrawMode.Normal;
		}

		private void UpdateUserSelectTreeView()
		{
			this.treeViewUserSelect.Nodes.Clear();
			for (int i = 0; i < this.m_UserSelectTypeGroupList.Count; i++)
			{
				TreeNode treeNode = new TreeNode(this.m_UserSelectTypeGroupList[i].GroupName);
				treeNode.Tag = i;
				this.treeViewUserSelect.Nodes.Add(treeNode);
				for (int j = 0; j < this.m_UserSelectTypeGroupList[i].LayerTypeList.Count; j++)
				{
					WallTypeLayerInfos wallTypeLayerInfos = this.m_UserSelectTypeGroupList[i].LayerTypeList[j];
					this.UpdateViewTypeCompoundStructure(wallTypeLayerInfos, j, treeNode);
				}
				treeNode.Expand();
			}
			int index = 0;
			int index2 = 0;
			this.treeViewUserSelect.SelectedNode = this.treeViewUserSelect.Nodes[index].Nodes[index2];
		}

		private void UpdateUserNewTreeView(bool isReset)
		{
			if (isReset)
			{
				WallTypeMergeForm.OperationType operationType = this.CurrentOperationType();
				if (operationType == WallTypeMergeForm.OperationType.OT_NotMerge)
				{
					this.SetNotMergeData();
				}
				else if (operationType == WallTypeMergeForm.OperationType.OT_MergeToOne)
				{
					this.MergeToOne();
				}
				else
				{
					this.MergeByCustom();
				}
			}
			this.treeViewUserNew.Nodes.Clear();
			int num = this.TypeGroupNodeIndex();
			List<List<WallTypeLayerInfos>> currentWallTypeGroupInfos = this.GetCurrentWallTypeGroupInfos(num);
			for (int i = 0; i < currentWallTypeGroupInfos.Count; i++)
			{
				string text = "HY_";
				for (int j = 0; j < currentWallTypeGroupInfos[i].Count; j++)
				{
					WallTypeLayerInfos wallTypeLayerInfos = currentWallTypeGroupInfos[i][j];
					text = text + wallTypeLayerInfos.TypeName + " ";
				}
				TreeNode treeNode = new TreeNode(text);
				this.treeViewUserNew.Nodes.Add(treeNode);
				for (int j = 0; j < currentWallTypeGroupInfos[i].Count; j++)
				{
					WallTypeLayerInfos wallTypeLayerInfos2 = currentWallTypeGroupInfos[i][j];
					this.UpdateViewTypeCompoundStructure(wallTypeLayerInfos2, j, treeNode);
				}
				treeNode.Expand();
			}
			this.groupBoxNew.Text = this.m_UserSelectTypeGroupList[num].GroupName;
		}

		private List<List<WallTypeLayerInfos>> GetCurrentWallTypeGroupInfos(int groupIndex)
		{
			List<List<WallTypeLayerInfos>> list = new List<List<WallTypeLayerInfos>>();
			List<List<int>> list2 = this.m_GroupCombinationInfosList[groupIndex];
			WallTypeGroupInfos wallTypeGroupInfos = this.m_UserSelectTypeGroupList[groupIndex];
			foreach (List<int> list3 in list2)
			{
				List<WallTypeLayerInfos> list4 = new List<WallTypeLayerInfos>();
				foreach (int index in list3)
				{
					list4.Add(wallTypeGroupInfos.LayerTypeList[index]);
				}
				list.Add(list4);
			}
			return list;
		}

		private WallTypeMergeForm.OperationType CurrentOperationType()
		{
			if (this.rdbNotMerge.Checked)
			{
				return WallTypeMergeForm.OperationType.OT_NotMerge;
			}
			if (this.rdbMergeToOne.Checked)
			{
				return WallTypeMergeForm.OperationType.OT_MergeToOne;
			}
			return WallTypeMergeForm.OperationType.OT_Custom;
		}

		private void SetNotMergeData()
		{
			int index = this.TypeGroupNodeIndex();
			this.m_GroupCombinationInfosList[index].Clear();
			int num = 0;
			foreach (WallTypeLayerInfos wallTypeLayerInfos in this.m_UserSelectTypeGroupList[index].LayerTypeList)
			{
				List<int> list = new List<int>();
				list.Add(num);
				this.m_GroupCombinationInfosList[index].Add(list);
				num++;
			}
		}

		private void MergeToOne()
		{
			int index = this.TypeGroupNodeIndex();
			List<int> list = new List<int>();
			this.m_GroupCombinationInfosList[index].Clear();
			int num = 0;
			foreach (WallTypeLayerInfos wallTypeLayerInfos in this.m_UserSelectTypeGroupList[index].LayerTypeList)
			{
				list.Add(num);
				num++;
			}
			this.m_GroupCombinationInfosList[index].Add(list);
		}

		private void MergeByCustom()
		{
			this.SetNotMergeData();
		}

		private void UpdateViewTypeCompoundStructure(WallTypeLayerInfos wallTypeLayerInfos, int index, TreeNode parentNode)
		{
			TreeNode treeNode = new TreeNode(wallTypeLayerInfos.TypeName);
			treeNode.Tag = index;
			parentNode.Nodes.Add(treeNode);
		}

		private void rdbNotMerge_Click(object sender, EventArgs e)
		{
			if (this.rdbNotMerge.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void rdbMergeToOne_Click(object sender, EventArgs e)
		{
			if (this.rdbMergeToOne.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void rdbCustom_Click(object sender, EventArgs e)
		{
			if (this.rdbCustom.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void OperationTypeChanged()
		{
			int index = this.TypeGroupNodeIndex();
			if (this.rdbNotMerge.Checked)
			{
				this.btnCombination.Enabled = false;
				this.btnDecomposition.Enabled = false;
				this.treeViewUserNew.CheckBoxes = false;
				this.treeViewUserNew.DrawMode = TreeViewDrawMode.Normal;
				this.m_OperationTypeByTypeList[index] = WallTypeMergeForm.OperationType.OT_NotMerge;
			}
			else if (this.rdbMergeToOne.Checked)
			{
				this.btnCombination.Enabled = false;
				this.btnDecomposition.Enabled = false;
				this.treeViewUserNew.CheckBoxes = false;
				this.treeViewUserNew.DrawMode = TreeViewDrawMode.Normal;
				this.m_OperationTypeByTypeList[index] = WallTypeMergeForm.OperationType.OT_MergeToOne;
			}
			else
			{
				this.btnCombination.Enabled = true;
				this.btnDecomposition.Enabled = true;
				this.treeViewUserNew.CheckBoxes = true;
				this.treeViewUserNew.DrawMode = TreeViewDrawMode.OwnerDrawAll;
				this.m_OperationTypeByTypeList[index] = WallTypeMergeForm.OperationType.OT_Custom;
			}
			this.UpdateUserNewTreeView(true);
		}

		private void treeViewUserNew_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			if (this.CurrentOperationType() != WallTypeMergeForm.OperationType.OT_Custom)
			{
				return;
			}
			if (this.treeViewUserNew.Nodes.Count == 0)
			{
				return;
			}
			if (e.Node != null && e.Node.Parent != null)
			{
				TreeViewCheckBoxHideController.HideCheckBox(e.Node);
			}
			e.DrawDefault = true;
		}

		private void btnCombination_Click(object sender, EventArgs e)
		{
			List<int> list = new List<int>();
			if (!this.GetContinuousCheckedTypeNodesIndex(ref list))
			{
				MessageBox.Show("合并项不连续，无法合并", "盈建科提示");
				return;
			}
			int index = this.TypeGroupNodeIndex();
			List<int> list2 = new List<int>();
			for (int i = list.Count - 1; i >= 0; i--)
			{
				int index2 = list[i];
				list2.InsertRange(0, this.m_GroupCombinationInfosList[index][index2]);
				this.m_GroupCombinationInfosList[index].RemoveAt(index2);
			}
			this.m_GroupCombinationInfosList[index].Insert(list[0], list2);
			this.UpdateUserNewTreeView(false);
		}

		private bool GetContinuousCheckedTypeNodesIndex(ref List<int> continuousChecedNodesIndex)
		{
			List<TreeNode> list = new List<TreeNode>();
			List<int> list2 = new List<int>();
			for (int i = 0; i < this.treeViewUserNew.Nodes.Count; i++)
			{
				TreeNode treeNode = this.treeViewUserNew.Nodes[i];
				if (treeNode.Checked)
				{
					list.Add(treeNode);
					list2.Add(i);
				}
			}
			if (list.Count < 2)
			{
				return false;
			}
			for (int i = 0; i < list2.Count - 1; i++)
			{
				if (list2[i + 1] - list2[i] != 1)
				{
					return false;
				}
			}
			continuousChecedNodesIndex.AddRange(list2);
			return true;
		}

		private void btnDecomposition_Click(object sender, EventArgs e)
		{
			List<int> checkedTypeNodesIndex = this.GetCheckedTypeNodesIndex();
			int index = this.TypeGroupNodeIndex();
			for (int i = checkedTypeNodesIndex.Count - 1; i >= 0; i--)
			{
				int num = checkedTypeNodesIndex[i];
				List<int> list = this.m_GroupCombinationInfosList[index][num];
				this.m_GroupCombinationInfosList[index].RemoveAt(num);
				int num2 = 0;
				foreach (int item in list)
				{
					List<int> list2 = new List<int>();
					list2.Add(item);
					this.m_GroupCombinationInfosList[index].Insert(num + num2, list2);
					num2++;
				}
			}
			this.UpdateUserNewTreeView(false);
		}

		private List<int> GetCheckedTypeNodesIndex()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.treeViewUserNew.Nodes.Count; i++)
			{
				TreeNode treeNode = this.treeViewUserNew.Nodes[i];
				if (treeNode.Checked)
				{
					list.Add(i);
				}
			}
			return list;
		}

		private void treeViewUserSelect_AfterSelect(object sender, TreeViewEventArgs e)
		{
			int index = this.TypeGroupNodeIndex();
			this.SetCurrentOperationType(this.m_OperationTypeByTypeList[index]);
			this.UpdateUserNewTreeView(false);
		}

		private int TypeGroupNodeIndex()
		{
			TreeNode treeNode = this.treeViewUserSelect.SelectedNode;
			string value;
			if (treeNode.Parent == null)
			{
				value = treeNode.Tag.ToString();
			}
			else
			{
				treeNode = treeNode.Parent;
				value = treeNode.Tag.ToString();
			}
			return Convert.ToInt32(value);
		}

		private Document m_Doc;

		private List<WallTypeGroupInfos> m_UserSelectTypeGroupList = new List<WallTypeGroupInfos>();

		private List<WallTypeMergeForm.OperationType> m_OperationTypeByTypeList = new List<WallTypeMergeForm.OperationType>();

		private List<List<List<int>>> m_GroupCombinationInfosList = new List<List<List<int>>>();

		private enum OperationType
		{
			OT_NotMerge = -1,
			OT_MergeToOne,
			OT_Custom
		}
	}
}
